#include <QStandardItemModel>
#include <QVariant>
#include "ViewImage.h"
#include "ui_ViewImage.h"
#include "RawImage.h"
#include "ImagePanel.h"
#include "MultiLayout.h"

ViewImage::ViewImage(QWidget *parent) :
	QWidget(parent),
	ui(new Ui::ViewImage), m_pImagePanel(0)
{
	ui->setupUi(this);

	this->changeLayout();//default 1*1 layout
	connect(ui->btnLayout11, SIGNAL(clicked()), SLOT(SlotLayout11()));
	connect(ui->btnLayout12, SIGNAL(clicked()), SLOT(SlotLayout12()));
	connect(ui->btnLayout22, SIGNAL(clicked()), SLOT(SlotLayout22()));
}

ViewImage::~ViewImage()
{
	delete ui;
}

void ViewImage::setCaptureList(const QList<SCapture> &listCapture)
{
	QStandardItemModel *pModel = new QStandardItemModel;

	foreach (const SCapture &capture, listCapture)
	{
		QStandardItem *pItem = new QStandardItem;
		RawImage image(capture.m_strFilepath);
		QVariant var;
		QSize sizeThumb(100, 100);

		if(image.isNull()) var.setValue<QImage>(image);
		else var.setValue<QImage>(image.scaled(sizeThumb, Qt::KeepAspectRatio));

		pItem->setData(var, Qt::UserRole + 1);
		pItem->setData(capture.m_iPosition, Qt::UserRole + 2);
		pItem->setData(capture.m_strFilepath, Qt::UserRole + 4);
		pItem->setData(QString("%1").arg(capture.m_iPosition), Qt::ToolTipRole);
		pItem->setData(sizeThumb, Qt::SizeHintRole);
		pModel->appendRow(pItem);
	}

	ui->listView->setModel(pModel);
}

void ViewImage::SlotLayout11()
{
	this->changeLayout();
}

void ViewImage::SlotLayout12()
{
	this->changeLayout(1, 2);
}

void ViewImage::SlotLayout22()
{
	this->changeLayout(2, 2);
}

void ViewImage::changeLayout(int iRow, int iColumn)
{
	emit SignalHistory(NULL, NULL);

	if(m_pImagePanel)
	{
		ui->gridLayout->takeAt(ui->gridLayout->indexOf(m_pImagePanel));
		m_pImagePanel->reset();
		delete m_pImagePanel;
	}

	m_pImagePanel = new MultiLayout(iRow, iColumn, this);
	ui->gridLayout->addWidget(m_pImagePanel, 0, 0, 1, 1);

	connect(this, SIGNAL(SignalWindowing(int,int)), m_pImagePanel, SLOT(SlotWindowing(int,int)));
	connect(this, SIGNAL(SignalGamma(int)), m_pImagePanel, SLOT(SlotGamma(int)));
	connect(this, SIGNAL(SignalSetTool(ETool)), m_pImagePanel, SLOT(SlotSetTool(ETool)));
	connect(m_pImagePanel, SIGNAL(SignalWindowingValue(int,int,int,int)), SIGNAL(SignalWindowingValue(int,int,int,int)));
	connect(m_pImagePanel, SIGNAL(SignalHistogramData(QMap<int,int>)), SIGNAL(SignalHistogramData(QMap<int,int>)));
	connect(m_pImagePanel, SIGNAL(SignalProfileDraw(QList<int>)), SIGNAL(SignalProfileDraw(QList<int>)));
	connect(this, SIGNAL(SignalSaveImage()), m_pImagePanel, SLOT(SlotSaveCurrent()));
	connect(this, SIGNAL(SignalSaveDiagnosis()), m_pImagePanel, SLOT(SlotSaveDiagnosis()));
	connect(m_pImagePanel, SIGNAL(SignalHistory(QAction*,QAction*)), SIGNAL(SignalHistory(QAction*,QAction*)));
}
